package com.cqc.jdk8.Jutil.stream;

import com.cqc.eb.UserEB;
import com.cqc.eb.UserDTO;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cui
 * @date 2024/6/30 13:20
 * @description
 **/
public class MapTest {

    List<String> stringList = null;
    List<UserEB> userEBList1 = null;

    @Before
    public void before() {
        //串行 并行
        stringList = getStringList();
        userEBList1 = getUserList1();
    }

    /**
     * BeanUtils.copyProperties 需要  commons-logging-1.2.jar commons-collections-3.2.2.jar
     */
    @Test
    public void map() {
//        stringList.stream()
//                .map(String::toUpperCase)
//                .sorted((a,b)->b.compareTo(a))
//                .forEach(System.out::println);

        List<UserDTO> collect = userEBList1.stream()
                .map(user -> {
                    UserDTO dto = new UserDTO();
                    try {
                        BeanUtils.copyProperties(dto, user);
                        dto.setAge(dto.getAge() + 10);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return dto;
                })
                .collect(Collectors.toList());
    }

    private static List<String> getStringList() {
        List<String> stringCollection = new ArrayList<>();
        stringCollection.add("ddd2");
        stringCollection.add("aaa2");
        stringCollection.add("bbb1");
        stringCollection.add("aaa1");
        stringCollection.add("bbb3");
        stringCollection.add("ccc1");
        stringCollection.add("bbb2");
        stringCollection.add("ddd1");
        return stringCollection;
    }

    private static List<UserEB> getUserList1() {
        List<UserEB> list = new ArrayList<>();
        UserEB w = new UserEB("w", 10);
        UserEB x = new UserEB("x", 11);
        UserEB y = new UserEB("y", 12);
        list.add(w);
        list.add(x);
        list.add(y);
        return list;
    }
}
